<template>
    <div class="flip-content">
        <div class="some-element">
            <img 
                ref="imgBoxRef" 
                @click="imgClick" 
                src="https://picsum.photos/id/64/4326/2884" 
                class="img-element">
        </div>
        <div @click="previewBoxClick" ref="previewBoxRef" class="preview-box">
            <div ref="detailItemRef" class="detail">
                <img/>
            </div>
        </div>
    </div>
</template>

<script setup>
    import { ref, onActivated, onMounted } from 'vue';
    const previewBoxRef = ref(null);
    const firstRect = ref(null); 
    const lastRect = ref(null);
    const detailItemRef = ref(null);
    const imgBoxRef = ref(null);

    // 点击图片事件
    const imgClick = evt => {
        // 获取初始状态Frist
        firstRect.value = evt.target.getBoundingClientRect()

        // 获取预览图片dom
        const newImg = detailItemRef.value.querySelector('img')
        newImg.setAttribute("src", evt.target.getAttribute("src"))

        // 设置弹窗可见
        previewBoxRef.value.style.display = "block"
        imgBoxRef.value.style.opacity = 0

        // 获取结束状态last
        lastRect.value = detailItemRef.value.getBoundingClientRect()

        // 计算invert和播放play动画，以预览明细盒子为对象
        playAnimate(detailItemRef)
    }

    // 点击预览盒子
    const previewBoxClick = evt => {
        // 获取first，预览盒子图片
        firstRect.value = detailItemRef.value.getBoundingClientRect()
        // 获取last
        lastRect.value = imgBoxRef.value.getBoundingClientRect()

        // 设置弹窗不可见
        previewBoxRef.value.style.display = "none"
        imgBoxRef.value.style.opacity = 1

        // 计算invert和播放play动画，以原图片位置为对象
        playAnimate(imgBoxRef)
    }

    // 计算差值并播放动画
    const playAnimate = Element => {
        // Invert 获得动画起点和终点的差值，激活过渡动画
        const deltaX = firstRect.value.left - lastRect.value.left;
        const deltaY = firstRect.value.top - lastRect.value.top;
        const deltaW = firstRect.value.width / lastRect.value.width;
        const deltaH = firstRect.value.height / lastRect.value.height;

        // Play: 对最终元素从第一个边界到最后一个边界进行动画处理（不进行变换）
        Element.value.animate(
            [
            { 
                transform: `
                    translateX(${deltaX}px)
                    translateY(${deltaY}px)
                    scale(${deltaW})
                    ` 
                },
                { transform: `translateX(0) translateY(0) scale(1)` }
            ],
            {
                duration: 600,
                easing: "cubic-bezier(0.2, 0, 0.2, 1)"
            }
        );
    }

    onMounted(() => {
        previewBoxRef.value.style.display = "none"
    })
</script>

<style lang="scss" scoped>
.flip-content {
    display: flex;
    justify-content: center;
}
.some-element {
    max-width: 300px;
    max-height: 300px;
    background: #10ac84;
    border-radius: 12px;
    margin-bottom: 50px;
    line-height: 0;
}
.img-element {
    filter: grayscale(60%);
    width: 100%;
    height: 100%;
    border-radius: 12px;
    &:hover {
        filter: grayscale(0%);
        cursor: pointer;
    }
}

.preview-box {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    transition: opacity .36s ease-in-out;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.8);
}
.preview-box img {
    border-radius: 5px;
    width: 100%;
    height: 77%;
}
.detail {
    width: 82vmin;
    height: 78vmin;
    transform-origin: top left;
    display: flex;
    flex-direction: column;
    margin: 100px auto;
}
</style>